using System.Collections.Generic;
using UnityEngine;

namespace NodeCanvas.Actions
{
	[Category("Animation")]
	[AgentType(typeof(Animation))]
	public class PlayAnimationSimple : ActionTask
	{
		[RequiredField]
		public AnimationClip animationClip;

		[SliderField(0f, 1f)]
		public float crossFadeTime = 0.25f;

		public WrapMode animationWrap = WrapMode.Loop;

		public bool waitUntilFinish;

		private static Dictionary<Animation, AnimationClip> lastPlayedClips = new Dictionary<Animation, AnimationClip>();

		protected override string info
		{
			get
			{
				return "Anim '" + ((!animationClip) ? "NULL" : animationClip.name) + "'";
			}
		}

		protected override string OnInit()
		{
			base.agent.GetComponent<Animation>().AddClip(animationClip, animationClip.name);
			return null;
		}

		protected override void OnExecute()
		{
			if (lastPlayedClips.ContainsKey(base.agent.GetComponent<Animation>()) && lastPlayedClips[base.agent.GetComponent<Animation>()] == animationClip)
			{
				EndAction(true);
				return;
			}
			lastPlayedClips[base.agent.GetComponent<Animation>()] = animationClip;
			base.agent.GetComponent<Animation>()[animationClip.name].wrapMode = animationWrap;
			base.agent.GetComponent<Animation>().CrossFade(animationClip.name, crossFadeTime);
			if (!waitUntilFinish)
			{
				EndAction(true);
			}
		}

		protected override void OnUpdate()
		{
			if (base.elapsedTime >= animationClip.length - crossFadeTime)
			{
				EndAction(true);
			}
		}
	}
}
